package com.bdqn.action;

import com.bdqn.BaseResponse.BaseResponse;
import com.bdqn.pojo.Book;
import com.bdqn.pojo.Student;
import com.bdqn.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.List;

/**
 * 类名 StudentAction
 * 描述 TODO
 * 日期 2022/2/14 10:01
 * 创建人 by xfq
 * 项目名称 springbootbook
 */
@RestController
@CrossOrigin
public class StudentAction {
    //service层资源
    @Autowired
    private StudentService studentService;
    //注册
    @RequestMapping("/addStudent")
    public BaseResponse<Student> addStudent(Integer sno, String password, Character gender,String sname, String address, String phone, Date birthday){
        Student student = new Student();
        student.setSno(sno);
        student.setPassword(password);
        student.setGender(gender);
        student.setAddress(address);
        student.setSname(sname);
        student.setPhone(phone);
        student.setBirthday(birthday);
        BaseResponse<Student> resp = new BaseResponse<Student>();
        if (studentService.addStudent(student)>0){
            resp.setMsg("新增成功");
        }else {
            resp.setMsg("新增失败");
        }
        return resp;
    }
    //登录
    @RequestMapping("/sumStu")
    public BaseResponse<Student> sumStu(Integer sno,Integer password){
        BaseResponse<Student> resp = new BaseResponse<Student>();
        resp.setMsg(studentService.sumStu(sno, password)?"登录成功!":"登录失败!");
        return resp;
    }
    //书籍页面展示
    @RequestMapping("/sumBook")
    public BaseResponse<List<Book>> sumBook(){
        BaseResponse<List<Book>> resp = new BaseResponse<List<Book>>();
        resp.setRows(studentService.sumBook());
        return resp;
    }
    //动态条件书籍查询
    @RequestMapping("/listBook")
    public BaseResponse<List<Book>> listBook(String title,String author){
        BaseResponse<List<Book>> resp = new BaseResponse<List<Book>>();
        resp.setRows(studentService.listBook(title,author));
        return resp;
    }
    //新增书籍
    @RequestMapping("/addBook")
    public BaseResponse<Book> addBook(String title, String author, String pshouse,Integer ISBN,Date psbirthday,Integer inventory,Double price){
        Book book = new Book();
        book.setTitle(title);
        book.setAuthor(author);
        book.setPshouse(pshouse);
        book.setISBN(ISBN);
        book.setPsbirthday(psbirthday);
        book.setInventory(inventory);
        book.setPrice(price);
        BaseResponse<Book> resp = new BaseResponse<Book>();
        resp.setMsg(studentService.addBook(book)?"新增成功!":"新增失败!");
        return resp;
    }
}
